import { defineComponent, reactive, ref, toRefs } from 'vue'
import { NForm, NFormItem, NModal, FormRules, NTreeSelect } from 'naive-ui'
import { Response } from '@/service/modules/common/types'
import { useDepartment } from '@/views/department/tab/department-manage-tab/use-department'
import { updateProjectDepartmentReq } from '@/service/modules/project'

const ModProjectDepartmentModal = defineComponent({
  name: 'ModProjectDepartmentModal',
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    proId: {
      type: Number,
      required: true,
      default: 0
    }
  },
  emits: ['modalShowChange', 'refreshList'],
  setup() {
    const state = reactive({
      form: {
        proId: 0,
        departmentId: null
      },
      positiveButtonDisabled: ref(false),
      rules: {
        departmentId: {
          trigger: ['input', 'blur'],
          required: true,
          validator() {
            if (!state.form.departmentId) {
              return new Error(window.$t('user.departmentTips'))
            }
          }
        }
      } as FormRules
    })

    //部门树下拉选项
    const { departmentVariables, getDepartmentOptions } = useDepartment()
    getDepartmentOptions()
    return {
      ...toRefs(state),
      departmentVariables
    }
  },
  methods: {
    async onPositiveClick() {
      //输入框存在为空时则确认按钮无效
      if (!this.form.departmentId) {
        return
      }
      //提交修改部门请求
      this.positiveButtonDisabled = true
      this.form.proId = this.proId
      const res: Response<any> = await updateProjectDepartmentReq({
        ...this.form
      }).catch(() => {
        this.positiveButtonDisabled = false
      })
      if (res.resp_code === 0) {
        this.initData()
        this.$emit('modalShowChange', false)
        this.$emit('refreshList')
        this.positiveButtonDisabled = false
      } else {
        this.positiveButtonDisabled = false
      }
    },
    onNegativeClick() {
      this.initData()
      this.$emit('modalShowChange', false)
    },
    initData() {
      this.form.departmentId = null
    }
  },
  render() {
    return (
      <NModal
        show={this.showModal}
        mask-closable={false}
        style='width: 400px'
        title={window.$t('department.editDepartment')}
        bordered={false}
        size='huge'
        preset='dialog'
        positive-text={window.$t('dialog.positiveText')}
        negative-text={window.$t('dialog.negativeText')}
        onPositiveClick={this.onPositiveClick}
        onNegativeClick={this.onNegativeClick}
        positiveButtonProps={{
          disabled: this.positiveButtonDisabled || !this.form.departmentId
        }}
        onClose={this.onNegativeClick}
      >
        <NForm
          rules={this.rules}
          label-placement='left'
          require-mark-placement='right-hanging'
          label-width='auto'
        >
          <NFormItem path='departmentId' label={window.$t('user.department')}>
            <NTreeSelect
              v-model={[this.form.departmentId, 'value']}
              options={this.departmentVariables.departmentOptions}
              label-field='name'
              key-field='id'
              children-field='subs'
              clearable
              size='small'
              placeholder={window.$t('user.departmentTips')}
            />
          </NFormItem>
        </NForm>
      </NModal>
    )
  }
})

export default ModProjectDepartmentModal
